/**
 * All Rights Reserved , Copyright (C) 2021 , 青岛鼎信通讯股份有限公司
 * <p>
 * MpmProjectStakeholderService 项目相关方
 * <p>
 * 修改纪录 2021-08-19 版本：1.0 liuchenguang 创建。
 *
 * @version 版本：1.0
 * @author 作者：liuchenguang 创建日期：2021-08-19
 */
package com.topscomm.mpm.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.topscomm.basic.BasicEntity;
import com.topscomm.basic.exception.ServiceException;
import com.topscomm.cbo.service.impl.CboDeptService;
import com.topscomm.cbo.service.impl.CboUserService;
import com.topscomm.mpm.MpmSystemConst;
import com.topscomm.mpm.pojo.MpmProjectEntity;
import com.topscomm.mpm.pojo.MpmProjectTeamEntity;
import com.topscomm.mpm.pojo.MpmProjectStakeholderEntity;
import com.topscomm.pub.util.BaseUtil;
import com.topscomm.pub.util.ConvertUtil;
import com.topscomm.pub.util.StringBufferProxy;
import com.topscomm.pub.util.StringUtil;
import com.topscomm.pub.vo.Page;
import com.topscomm.pub.vo.Query;
import com.topscomm.pub.vo.ResponseResult;
import com.topscomm.tap.common.DatacodeCache;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.topscomm.mpm.service.IMpmProjectStakeholderService;
import com.topscomm.mpm.service.impl.auto.MpmProjectStakeholderServiceAuto;
import org.springframework.transaction.annotation.Transactional;

@Service(value = "mpmProjectStakeholderService")
@Scope(value = "singleton")
public class MpmProjectStakeholderService extends MpmProjectStakeholderServiceAuto implements
    IMpmProjectStakeholderService {

    @Autowired
    public CboUserService cboUserService;
    @Autowired
    public CboDeptService cboDeptService;
    @Autowired
    public MpmProjectService mpmProjectService;
    @Autowired
    public MpmProjectTaskService mpmProjectTaskService;
    @Autowired
    public MpmProjectTeamService mpmProjectTeamService;

    DatacodeCache datacodeCache = new DatacodeCache();


    /**
     * @param
     * @param sqlWhere 转换后的查询语句
     * @return
     * @author: liuchenguang
     * @date: 2021-08-11
     * @description: 获取列表（空值放最后）
     * @modify:
     */
    public Page listRevokeSpace(Map<String, Object> paraMap, String sqlWhere) {
        // 只改动排序，其余保持不变
        paraMap = BaseUtil.decodeSecureMap(paraMap);
        Query query = new Query(paraMap);
        int totalCount = this.getCount(sqlWhere);
        List<Map<String, Object>> tempList = null;
        Map<String, Object> paraMap2 = new HashMap<>();
        int begincount = query.getCurrentPage() * query.getPageSize() - query.getPageSize();
        paraMap2.put("begincount", begincount);
        paraMap2.put("pagesize", query.getPageSize());
        paraMap2.put("whereSql", sqlWhere);
        // 重写排序，添加默认排序：按影响程度倒序、工作组顺序排序，项目领导组、项目核心组排前面，各组组长排前面
        String sidxCode = query.getSidx();
        if (StringUtil.isEmpty(sidxCode)) {
            tempList = this.queryMapForPage(paraMap2, "defaultSortQueryMapForPage");
        } else {
            // 空值永远排最后
            StringBufferProxy sidxSql = new StringBufferProxy();
            sidxSql.appendSingle("case when {0} is null then 1 else 0 end, {0}",
                query.getSidx());
            paraMap2.put("sidx", sidxSql.toString());
            paraMap2.put("sord", query.getSord());
            if (query.getPageSize() > 0) {
                tempList = this.queryMapForPage(paraMap2, "queryMapForPage");
            } else {
                tempList = this.queryMap(paraMap2, "queryMapByWhere");
            }
        }
        // 如果项目相关方存在，查询相关方人员参加项目数量和负责任务数量
        if (!CollectionUtil.isEmpty(tempList)) {
            mpmProjectTeamService.queryProjectNum(tempList);
            mpmProjectTaskService.queryTaskNum(tempList);
        }
        this.setDispFields(tempList);
        return new Page(tempList, totalCount, query.getPageSize(), query.getCurrentPage());
    }

    /**
     * @param
     * @param sqlWhere 转换后的查询语句
     * @return
     * @author: liuchenguang
     * @date: 2021-08-25
     * @description: refList用，去除重复人员
     * @modify:
     */
    public Page listRevokeRepeat(Map<String, Object> paraMap, String sqlWhere) {
        paraMap = BaseUtil.decodeSecureMap(paraMap);
        Query query = new Query(paraMap);
        Map<String, Object> countMap = new HashMap<>();
        countMap.put("whereSql", sqlWhere);
        int totalCount = this.getCount(countMap, "distinctGetCount");
        List<Map<String, Object>> tempList = null;
        Map<String, Object> paraMap2 = new HashMap<>();
        int begincount = query.getCurrentPage() * query.getPageSize() - query.getPageSize();
        paraMap2.put("begincount", begincount);
        paraMap2.put("pagesize", query.getPageSize());
        // 重写排序，空值永远排最后
        StringBufferProxy sidxSql = new StringBufferProxy();
        // 默认按照工作组编码排序
        String sidxCode = StringUtil.isEmpty(query.getSidx()) ? "messagecode" : query.getSidx();
        sidxSql.appendSingle("case when {0} is null then 1 else 0 end, {0}",
            sidxCode);
        paraMap2.put("sidx", sidxSql.toString());
        // 默认倒序排列
        String sordCode = StringUtil.isEmpty(query.getSord()) ? "desc" : query.getSord();
        paraMap2.put("sord", sordCode);
        paraMap2.put("whereSql", sqlWhere);
        // 重写查询，添加去重
        if (query.getPageSize() > 0) {
            tempList = this.queryMapForPage(paraMap2, "distinctQueryMapForPage");
        } else {
            tempList = this.queryMap(paraMap2, "distinctQueryMapByWhere");
        }
        // 如果项目相关方存在，查询相关方人员参加项目数量和负责任务数量
        if (!CollectionUtil.isEmpty(tempList)) {
            mpmProjectTeamService.queryProjectNum(tempList);
            mpmProjectTaskService.queryTaskNum(tempList);
        }
        this.setDispFields(tempList);
        return new Page(tempList, totalCount, query.getPageSize(), query.getCurrentPage());
    }

    /**
     * @param
     * @return
     * @author: liuchenguang
     * @date: 2021-08-24
     * @description: 获取工作组分类的内部人员列表
     * @modify:
     */
    public Map<String, Object> getGroupList(Map<String, Object> paraMap) {
        // 查询内部的有效人员，可带项目编号
        StringBufferProxy sql = new StringBufferProxy();
        sql.appendSingle(" enabled = 1  and {0} = '1' ", MpmProjectStakeholderEntity.FieldStakeholdercategory); // 表示内部人员
        // 查询参数，项目编号或id
        if (ObjectUtil.isEmpty(paraMap.get("qry_projectcode"))) {
            MpmProjectEntity mpmProjectEntity = mpmProjectService.queryById(ConvertUtil.convertToString(paraMap.get("id")));
            paraMap.put("qry_projectcode", mpmProjectEntity.getProjectcode());
        }
        String value = ConvertUtil.convertToString(paraMap.get("qry_projectcode"));
        sql.appendSingle(" and projectcode = '{0}' ", value);
        // 查询数据,同一个人保留最高级别职位
        List<Map<String, Object>> tempList = this.queryMapByWhere(sql.toString(), "defaultSortQueryMapMobile");
        List<Map<String, Object>> leaderContentList = new ArrayList<Map<String, Object>>(); // 领导组
        List<Map<String, Object>> decisionContentList = new ArrayList<Map<String, Object>>(); // 决策组
        List<Map<String, Object>> teamList = new ArrayList<Map<String, Object>>(); // 项目组
        List<Map<String, Object>> otherContentList = new ArrayList<Map<String, Object>>(); // 其他
        tempList = tempList.stream().filter(distinctByKey(item -> item.get("usercode"))).collect(Collectors.toList());
        for (Map<String, Object> temp : tempList) {
            // 工作组编码
            String workGroupCode = ConvertUtil.convertToString(temp.get("messagecode"));
            // 职位字段没有的话改为空
            if ("\\".equals(ConvertUtil.convertToString(temp.get("position")))) {
                temp.put("position", null);
            }
            // 工作组分类
            if (StringUtil.isEmpty(workGroupCode)) {
                otherContentList.add(temp); // 其他
            } else if (MpmSystemConst.WorkGroupCode.decision.equals(workGroupCode)) {
                decisionContentList.add(temp); // 决策组
            } else if (MpmSystemConst.WorkGroupCode.center.equals(workGroupCode)) {
                teamList.add(temp); // 项目核心组
            } else if (MpmSystemConst.WorkGroupCode.manager.equals(workGroupCode)) {
                leaderContentList.add(temp); // 领导组
            }
        }
        // 添加到返回的数据列表中
        List<Map<String, Object>> groupList = new ArrayList<Map<String, Object>>(); // 总数据列表dataList
        setGroupList(groupList, leaderContentList, MpmSystemConst.WorkGroupCode.manager);
        setGroupList(groupList, decisionContentList, MpmSystemConst.WorkGroupCode.decision);
        setGroupList(groupList, teamList, MpmSystemConst.WorkGroupCode.center);
        setGroupList(groupList, otherContentList, MpmSystemConst.WorkGroupCode.other);
        // 返回数据
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("dataList", groupList);
        return result;
    }

    // 过滤重复人员，保留第一个
    private static <T> Predicate<T> distinctByKey(Function<? super T,?> keyExtractor){
        Map<Object,Boolean> map=new ConcurrentHashMap<>();
        return t -> map.putIfAbsent(keyExtractor.apply(t),Boolean.TRUE)==null;
    }
    /**
     * @param groupList  总数据列表
     * @param memberList 组成员列表
     * @param type       要添加的组类型
     * @return map对象，包括成员列表（content）和工作组名称（group）
     * @author: liuchenguang
     * @date: 2021-9-1
     * @description: 根据类型添加人员
     * @modify:
     */
    private void setGroupList(List<Map<String, Object>> groupList,
        List<Map<String, Object>> memberList, String type) {
        if (memberList.isEmpty()) {
            return;
        }
        Map<String, Object> tempGroupMap = new HashMap<>();
        String group = datacodeCache
            .getValueDynamic("MpmStakeholderWorkGroup", type,
                "code", "name");
        tempGroupMap.put("group", group);
        tempGroupMap.put("content", memberList);
        this.setDispFields(memberList);
        // 返回数据
        groupList.add(tempGroupMap);
    }

    /**
     * @param mapRow
     * @param datacodeCache
     * @author: liuchenguang
     * @date: 2021-9-6
     * @description:重写setDispFields
     * @modify:
     */
    @Override
    public void setDispFields(Map<String, Object> mapRow, DatacodeCache datacodeCache) {
        super.setDispFields(mapRow, datacodeCache);
        // 添加app用部门字段
        mapRow.put("deptname", ConvertUtil.convertToString(mapRow.get("ORGDEPTARRAY")));
    }

    /**
     * @param entity
     * @throws
     * @author: liuchenguang
     * @date: 2021-8-18
     * @description:编号生成
     * @modify:
     */
    @Override
    protected void beforeInsert(BasicEntity entity) {
        super.beforeInsert(entity);
        GenerateIDAndCode(entity, "XG", "ordercode");
    }

    /**
     * @param header    : 编号头 如 PP PR PSl
     * @param codeField : 编号列名
     * @return
     * @throws
     * @description:
     * @author liuchenguang
     * @date 2021/9/11
     * @modify
     */
    @Transactional
    public void GenerateIDAndCode(BasicEntity entity, String header, String codeField) {
        try {
            //若id未生成 生成id
            if (entity.getId() <= 0) {
                mpmProjectService.GenerateID(entity);
            }
            String codeFieldStr = ConvertUtil.convertToString(entity.getValue(codeField));
            // 生成编码
            if (StringUtil.isEmpty(codeFieldStr)) {
                mpmProjectService.GenerateCode(entity, header, codeField);
            }
        } catch (Exception e) {
            throw new ServiceException(e + "编号生成失败!");
        }
    }

    /**
     * @param projectCode 项目编号
     * @return
     * @author: liuchenguang
     * @date: 2021-8-26
     * @description: 项目立项或项目负责人更改后修改相关方信息
     * @modify:
     */
    @Transactional(rollbackFor = Exception.class)
    public void addProjectLeaderInfo(String projectCode) {
        //查询项目entity
        StringBufferProxy whereSql = new StringBufferProxy();
        whereSql.appendLineSingle("projectcode = '{0}'", projectCode);
        List<MpmProjectEntity> mpmProjectEntities = mpmProjectService
            .queryByWhere(whereSql.toString());
        if (mpmProjectEntities.isEmpty()) {
            return;
        }
        MpmProjectEntity mpmProjectEntity = mpmProjectEntities.get(0);
        // 撤除相关方中已存在的领导组、决策组人员
        whereSql.clear();
        whereSql.appendSingle(
            " enabled = 1 and projectcode = '{0}' and messagecode in ('{1}','{2}')",
            projectCode, MpmSystemConst.WorkGroupCode.manager,
            MpmSystemConst.WorkGroupCode.decision);
        List<MpmProjectStakeholderEntity> tmpEntities = this.queryByWhere(whereSql.toString());
        List<BasicEntity> resultEntities = new ArrayList<BasicEntity>();
        for (MpmProjectStakeholderEntity tmpEntity : tmpEntities) {
            // 有效值设置为0
            tmpEntity.setEnabled(0);
            resultEntities.add(tmpEntity);
        }
        this.updateBatch(resultEntities);
        // 添加领导组人员
        addLeaderGroupMember(mpmProjectEntity, MpmSystemConst.WorkGroupRoleCode.pm);
        if (!StringUtil.isEmpty(mpmProjectEntity.getSponsorcode())) {
            addLeaderGroupMember(mpmProjectEntity, MpmSystemConst.WorkGroupRoleCode.sponsor);
        }
        addLeaderGroupMember(mpmProjectEntity, MpmSystemConst.WorkGroupRoleCode.owner);
        // 添加决策组人员
        addDecisionGroupMember(mpmProjectEntity);
    }

    /**
     * @param mpmProjectTeamEntity 该项目组人员
     * @return
     * @author: liuchenguang
     * @date: 2021-8-5
     * @description: 添加项目组人员信息
     * @modify:
     */
    @Transactional(rollbackFor = Exception.class)
    public MpmProjectStakeholderEntity addStakeholderInfo(MpmProjectTeamEntity mpmProjectTeamEntity) {
        MpmProjectStakeholderEntity item = new MpmProjectStakeholderEntity();
        // 判断人员是否存在相同的人员
        if (isExist(mpmProjectTeamEntity)) {
            return null;
        }
        // 设置项目基本信息
        setStakeholderBasicInfo(item, mpmProjectTeamEntity.getProjectcode());
        // 设置姓名、工号
        item.setUsercode(mpmProjectTeamEntity.getUsercode());
        item.setUsername(mpmProjectTeamEntity.getUsername());
        // 设置用户信息（部门、电话、邮箱、职位）
        setUserInfo(item, mpmProjectTeamEntity.getUsercode());
        // 设置工作组名称、工作组角色
        String groupRoleCode = mpmProjectTeamEntity.getProjectrole();
        item.setMessagecode(MpmSystemConst.WorkGroupCode.center); // 分类到项目组中
        item.setGrouprolecode(groupRoleCode);
        item.setMessage(mpmProjectTeamEntity.getWorkgroup());
        String groupRole = datacodeCache
            .getValueDynamic("MpmProjectRole", groupRoleCode,
                "code", "name");
        // 设置工作组角色
        item.setGrouprole(groupRole);
        // 设置备注
        List<Map<String, Object>> teamList = new ArrayList<>();
        teamList.add(item.convertToMap());
        mpmProjectTeamService.setDispFields(teamList);
        item.setDescription(ConvertUtil.convertToString(teamList.get(0).get("dispduty")));
        // 插入信息
        return item;
    }

    /**
     * @param mpmProjectTeamEntity 该项目组人员
     * @return
     * @author: liuchenguang
     * @date: 2021-9-13
     * @description: 判断是否存在相同的人员
     * @modify:
     */
    @Transactional(rollbackFor = Exception.class)
    public boolean isExist(MpmProjectTeamEntity mpmProjectTeamEntity) {
        boolean isExist = false;
        // 判断人员是否存在相同的人员
        StringBufferProxy whereSql = new StringBufferProxy();
        // 查询工号、项目编号、工作组、工作组角色
        whereSql.appendSingle(
            "usercode = '{0}' and projectcode = '{1}' and grouprolecode = '{2}' "
                + "and enabled = 1 and messagecode = '{3}'",
            mpmProjectTeamEntity.getUsercode(), mpmProjectTeamEntity.getProjectcode(),
            mpmProjectTeamEntity.getProjectrole(), MpmSystemConst.WorkGroupCode.center);
        List<Map<String, Object>> thisEntities = this
            .queryMapFieldsByWhere(whereSql.toString(), Arrays.asList("id"));
        if (!thisEntities.isEmpty()) {
            isExist = true;
        }
        return isExist;
    }

    /**
     * @param mpmProjectEntity 项目实体
     * @param roleCode         项目组中的角色
     * @return
     * @author: liuchenguang
     * @date: 2021-9-1
     * @description: 添加领导组人员
     * @modify:
     */
    @Transactional(rollbackFor = Exception.class)
    public void addLeaderGroupMember(MpmProjectEntity mpmProjectEntity, String roleCode) {
        MpmProjectStakeholderEntity item = new MpmProjectStakeholderEntity();
        // 设置项目基本信息
        setStakeholderBasicInfo(item, mpmProjectEntity.getProjectcode());
        // 设置影响程度为5
        item.setStakeholderimpact("5");
        // 设置工作组、工作组角色
        item.setMessagecode(MpmSystemConst.WorkGroupCode.manager);
        item.setGrouprolecode(roleCode);
        String group = datacodeCache
            .getValueDynamic("MpmStakeholderWorkGroup", MpmSystemConst.WorkGroupCode.manager,
                "code",
                "name");
        String groupRole = datacodeCache
            .getValueDynamic("MpmProjectRole", roleCode, "code", "name");
        item.setMessage(group);
        item.setGrouprole(groupRole);
        // 设置工号
        if (MpmSystemConst.WorkGroupRoleCode.pm.equals(roleCode)) {
            item.setUsercode(mpmProjectEntity.getProjectmanagercode());
            item.setUsername(mpmProjectEntity.getProjectmanager());
            // 设置用户信息（部门、电话、邮箱、职位）
            setUserInfo(item, mpmProjectEntity.getProjectmanagercode());
        } else if (MpmSystemConst.WorkGroupRoleCode.sponsor.equals(roleCode)) {
            item.setUsercode(mpmProjectEntity.getSponsorcode());
            item.setUsername(mpmProjectEntity.getSponsor());
            setUserInfo(item, mpmProjectEntity.getSponsorcode());
        } else if (MpmSystemConst.WorkGroupRoleCode.owner.equals(roleCode)) {
            item.setUsercode(mpmProjectEntity.getOwnercode());
            item.setUsername(mpmProjectEntity.getOwner());
            setUserInfo(item, mpmProjectEntity.getOwnercode());
        }
        this.insert(item);
    }

    /**
     * @param mpmProjectEntity 项目实体
     * @return
     * @author: liuchenguang
     * @date: 2021-9-1
     * @description: 添加决策组人员
     * @modify:
     */
    @Transactional(rollbackFor = Exception.class)
    public void addDecisionGroupMember(MpmProjectEntity mpmProjectEntity) {
        String decisionUserCode = mpmProjectEntity.getDecisionusercode();
        String decisionUserName = mpmProjectEntity.getDecisionusername();
        // 旧数据的决策组人员开头有个逗号，得去掉
        if (decisionUserCode.length() > 0 && ",".equals(decisionUserCode.substring(0, 1))) {
            decisionUserCode = decisionUserCode.substring(1);
        }
        if (decisionUserName.length() > 0 && ",".equals(decisionUserName.substring(0, 1))) {
            decisionUserName = decisionUserName.substring(1);
        }
        String[] decisionUserCodeList = decisionUserCode.split(","); // 决策组工号列表
        String[] decisionUserNameList = decisionUserName.split(","); // 决策组姓名号列表
        List<BasicEntity> resultList = new ArrayList<BasicEntity>();
        for (int i = 0; i < decisionUserCodeList.length; i++) {
            // 设置相关方信息
            MpmProjectStakeholderEntity item = new MpmProjectStakeholderEntity();
            // 设置项目基本信息
            setStakeholderBasicInfo(item, mpmProjectEntity.getProjectcode());
            // 设置影响程度为5
            item.setStakeholderimpact("5");
            // 设置工作组、工作组角色
            String roleCode = MpmSystemConst.WorkGroupRoleCode.groupWorker;
            item.setMessagecode(MpmSystemConst.WorkGroupCode.decision);
            item.setGrouprolecode(roleCode);
            String group = datacodeCache
                .getValueDynamic("MpmStakeholderWorkGroup", MpmSystemConst.WorkGroupCode.decision,
                    "code", "name");
            String groupRole = datacodeCache
                .getValueDynamic("MpmProjectRole", roleCode, "code", "name");
            item.setMessage(group);
            item.setGrouprole(groupRole);
            // 设置用户信息
            item.setUsername(decisionUserNameList[i]);
            String userCode = decisionUserCodeList[i];
            item.setUsercode(userCode);
            setUserInfo(item, userCode);
            // 插入决策组人员到相关方表
            resultList.add(item);
        }
        this.insertBatch(resultList);
    }

    /**
     * @param mpmProjectTeamEntity 该项目组人员
     * @return
     * @author: liuchenguang
     * @date: 2021-8-5
     * @description: 移除相关方信息
     * @modify:
     */
    @Transactional(rollbackFor = Exception.class)
    public void revokeStakeholderInfo(MpmProjectTeamEntity mpmProjectTeamEntity) {
        // 从相关方表中撤销人员
        String projectCodeFromTeam = mpmProjectTeamEntity.getProjectcode();
        String revokeUserCode = mpmProjectTeamEntity.getUsercode();
        String roleCode = mpmProjectTeamEntity.getProjectrole();
        StringBufferProxy whereSql = new StringBufferProxy();
        // 查询工号、项目编号、工作组、工作组角色，防止人员兼任时出错
        whereSql.appendSingle("usercode = '{0}' and projectcode = '{1}' and grouprolecode = '{2}' "
                + "and enabled = 1 ", revokeUserCode, projectCodeFromTeam, roleCode);
        if (isManager(roleCode)) {
            whereSql.appendSingle(" and messagecode = '{0}'", MpmSystemConst.WorkGroupCode.manager);
        } else if (isDecision(roleCode)) {
            whereSql.appendSingle(" and messagecode = '{0}'", MpmSystemConst.WorkGroupCode.decision);
        } else if (isCenter(roleCode)) {
            whereSql.appendSingle(" and message = '{0}'", mpmProjectTeamEntity.getWorkgroup());
        }
        List<MpmProjectStakeholderEntity> thisEntities = this.queryByWhere(whereSql.toString());
        if (thisEntities.isEmpty()) {
            return;
        }
        List<BasicEntity> resultEntities = new ArrayList<BasicEntity>();
        for (MpmProjectStakeholderEntity thisEntity : thisEntities) {
            // 有效值设置为0
            thisEntity.setEnabled(0);
            resultEntities.add(thisEntity);
        }
        this.updateBatch(resultEntities);
    }

    /**
     * @param roleCode 组内角色编码
     * @return
     * @author: liuchenguang
     * @date: 2021-9-2
     * @description: 判断是否是领导组成员
     * @modify:
     */
    public boolean isManager(String roleCode) {
        return MpmSystemConst.WorkGroupRoleCode.pm.equals(roleCode)
            || MpmSystemConst.WorkGroupRoleCode.sponsor.equals(roleCode)
            || MpmSystemConst.WorkGroupRoleCode.owner.equals(roleCode);
    }

    /**
     * @param roleCode 组内角色编码
     * @return
     * @author: liuchenguang
     * @date: 2021-9-2
     * @description: 判断是否是决策组成员
     * @modify:
     */
    public boolean isDecision(String roleCode) {
        return MpmSystemConst.WorkGroupRoleCode.decision.equals(roleCode);
    }

    /**
     * @param roleCode 组内角色编码
     * @return
     * @author: liuchenguang
     * @date: 2021-9-2
     * @description: 判断是否是项目组成员
     * @modify:
     */
    public boolean isCenter(String roleCode) {
        return MpmSystemConst.WorkGroupRoleCode.groupLeader.equals(roleCode)
            || MpmSystemConst.WorkGroupRoleCode.groupWorker.equals(roleCode);
    }

    /**
     * @param item        需要设置信息的相关方实体
     * @param projectCode 项目编号
     * @return
     * @author: liuchenguang
     * @date: 2021-9-2
     * @description: 设置相关方的基本信息
     * @modify:
     */
    private void setStakeholderBasicInfo(MpmProjectStakeholderEntity item, String projectCode) {
        //查询项目entity
        StringBufferProxy whereSql = new StringBufferProxy();
        whereSql.appendLineSingle("projectcode = '{0}'", projectCode);
        List<MpmProjectEntity> mpmProjectEntities = mpmProjectService
            .queryByWhere(whereSql.toString());
        if (mpmProjectEntities.isEmpty()) {
            return;
        }
        MpmProjectEntity mpmProjectEntity = mpmProjectEntities.get(0);
        // 设置项目信息、人员类别
        item.setProjectcode(projectCode);
        item.setStakeholdercategory("1"); // 表示内部人员
        item.setProjectname(mpmProjectEntity.getProjectname());
        item.setProjectlevel(mpmProjectEntity.getProjectlevel());
        item.setProjectmanagercode(mpmProjectEntity.getProjectmanagercode());
        item.setProjectmanagername(mpmProjectEntity.getProjectmanager());
    }

    /**
     * @param item     要插入的相关方实体
     * @param userCode 用户工号
     * @author: liuchenguang
     * @date: 2021-8-5
     * @description: 获取用户信息
     * @modify: 2021-9-2
     */
    private void setUserInfo(MpmProjectStakeholderEntity item,
        String userCode) {
        // 获取人员信息
        StringBufferProxy whereSql = new StringBufferProxy();
        whereSql.appendLineSingle("code = '{0}'", userCode);
        // 返回列
        List<String> fields = new ArrayList<String>();
        fields.add("mobile");
        fields.add("email");
        fields.add("title");
        fields.add("deptid");
        List<Map<String, Object>> userList = cboUserService
            .queryMapFieldsByWhere(whereSql.toString(), fields);
        if (userList.isEmpty()) {
            return;
        }
        Map<String, Object> user = userList.get(0);
        // 设置邮箱、电话信息\职位
        if (user.get("mobile") != null) {
            item.setPhonenumber(ConvertUtil.convertToString(user.get("mobile")));
        }
        if (user.get("email") != null) {
            item.setEmail(ConvertUtil.convertToString(user.get("email")));
        }
        // 职位
        String position =
            user.get("title") == null ? "\\" : ConvertUtil.convertToString(user.get("title"));
        item.setPosition(position);
        // 获取部门信息
        fields.clear();
        fields.add("fullname");
        Map<String, Object> fullNameMap = cboDeptService
            .queryFieldsById(ConvertUtil.convertToString(user.get("deptid")), fields);
        // 公司或部门
        String deptName = fullNameMap.get("fullname") == null ? "\\"
            : ConvertUtil.convertToString(fullNameMap.get("fullname"));
        item.setOrgdeptarray(deptName);
    }
    /**
     * @param id     项目主表id
     * @param usercodeList 用户工号
     * @author: houyuhang
     * @date: 2022-08-30
     * @description: 会议系统获取
     */
    public List<Map<String, Object>> queryStakeholderMeet(long id, List<String> usercodeList) {
        if (id == 0 || CollectionUtil.isEmpty(usercodeList)) {
            throw new ServiceException("用户信息错误，请联系管理员！");
        }
        StringBufferProxy sql = new StringBufferProxy();
        String usercode = usercodeList.stream().collect(Collectors.joining("','","'", "'"));
        sql.appendSingle(" {0}.id = '{1}' and {2}.enabled = '1' and {2}.{3} in ({4}) ", MpmProjectEntity.tableName, ConvertUtil.convertToString(id),
                MpmProjectStakeholderEntity.tableName, MpmProjectStakeholderEntity.FieldUsercode, usercode);
        return this.queryMapByWhere(sql.toString(), "userRoleForMeet");
    }
}
